/**
 * 
 */
package com.flymobi.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Administrator
 * 
 */
public class DB {
	private Connection conLocal;
	private Connection conRemote;

	public DB() {
		String CLASSFORNAME = "com.mysql.jdbc.Driver";
		String SERVANDDB1 = "jdbc:mysql://113.31.27.130:3306/huanqiuwap?useUnicode=true&amp;characterEncoding=gb2312";
		String USER1 = "root";
		String PWD1 = "cfdxcfdx";
		String SERVANDDB2 = "jdbc:mysql://60.28.15.177:3306/phpcmsv9?useUnicode=true&amp;characterEncoding=gb2312";
		String USER2 = "waphz0706";
		String PWD2 = "s2u#ilm7";
		try {
			Class.forName(CLASSFORNAME);
			conLocal = DriverManager.getConnection(SERVANDDB1, USER1, PWD1);
			conRemote = DriverManager.getConnection(SERVANDDB2, USER2, PWD2);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int updateLocal(String sql) {
		try {
			Statement stmt = conLocal.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			int result = stmt.executeUpdate(sql);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public ResultSet queryLocal(String sql) {
		try {
			Statement stmt = conLocal.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			ResultSet result = stmt.executeQuery(sql);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ResultSet queryRemote(String sql) {
		try {
			Statement stmt = conRemote.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			ResultSet result = stmt.executeQuery(sql);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void close(){
		if(conLocal!=null){
			try {
				conLocal.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if(conRemote!=null){
			try {
				conRemote.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
}
